package com.example.yuejian.yuejian.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.adapter.SearchAdapter;
import com.example.yuejian.yuejian.base.ModleListListener;
import com.example.yuejian.yuejian.bean.Masters;
import com.example.yuejian.yuejian.model.SeachModel;

import java.util.List;

public class SearchActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ImageView btn_back;
    private EditText et_search;
    private Button btn_search;


    private ModleListListener <Masters>listListener=new ModleListListener<Masters>() {
        @Override
        public void onResponse(List<Masters> l) {
            List<Masters> list=l;
            if (list==null)
            {
                Toast.makeText(getApplication(),"没有查找搜索内容",Toast.LENGTH_SHORT).show();
            }
            recyclerView.setAdapter(new SearchAdapter(getApplication(),list));
        }

        @Override
        public void onFail(String msg) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        back();
        initViews();
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });
    }
    private void initViews()
    { recyclerView=(RecyclerView)findViewById(R.id.id_search_recycleview);
        // GridLayoutManager gm = new GridLayoutManager(this.getActivity(),2);
        // 网格是的list布局   2列
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //recyclerView.setLayoutManager(gm);
        et_search=(EditText)findViewById(R.id.et_search_content);
        btn_search=(Button)findViewById(R.id.btn_search_ok);

    }
    private void back()
    {
        btn_back=(ImageView)findViewById(R.id.imgBtn_search_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
    private  void getData()
    {
        SeachModel userModel=new SeachModel();
        userModel.Search(et_search.getText().toString(),listListener);
    }
}
